package com.fmyexample.v2mcsecuritybootredis.controller;

import com.fmy.mcsecurity.token.TokenManager;
import com.fmy.mcsecurity.token.model.SecurityToken;
import com.fmyexample.v2mcsecuritybootredis.security.CuUserDetails;
import lombok.AllArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName TestController
 * @Description TODO 测试controller
 * @createTime 2020/4/24 17:49
 */
@RestController
@AllArgsConstructor
public class TestController {

    private final TokenManager tokenManager;

    public CuUserDetails get() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof CuUserDetails) {
            return (CuUserDetails) principal;
        }
        return null;
    }

    /**
     * 获取当前用户，登录token信息
     */
    @GetMapping("/auth/user-login-token")
    public List<SecurityToken> userLoginToken() {
        return tokenManager.getUserToToken("app", get().userId());
    }

    /**
     * 剔出用户
     */
    @PostMapping("/kick")
    public String kick(String token, @RequestParam(defaultValue = "抱歉，您被管理员剔出，请重新登录") String cause) {
        tokenManager.kick(token, cause);
        return "剔出成功";
    }

    /**
     * 剔出某个用户某个客户端所有登录
     */
    @PostMapping("/kick-user")
    public String kickUser(String userId, @RequestParam(defaultValue = "抱歉，您被管理员剔出，请重新登录") String cause) {
        tokenManager.kickUserKey("app", userId, cause);
        return "剔出成功";
    }

}
